<?php
namespace Admin\Controller;
use Think\Controller;

//---------------
//Author:Hua
//--------------

class UserController extends CommonController {
    /*后台用户列表首页*/
    public function index(){
        $list = D('User');
        $total = $list->count();
        $userlist = $list->order('id desc')->getAll();
        $this->assign('total',$total);
        $this->assign('list',$userlist);
        $this->display('index');
    }
    
    /*删除会员*/
    public function delete(){
        $id = $_GET['id'];
        $list = D('User');
        $list->delete($id);
        $this->redirect('index',3);
    }

    /*增加会员*/
    public function add(){
        if(IS_POST){
            $data['username'] = $_POST['username'];
            $data['sex'] = $_POST['sex'];
            $data['pass'] = md5($_POST['pass']);
            $data['state'] = $_POST['state'];
            $data['addtime'] = time();
            $user = D('User');
            if($_POST['pass']==$_POST['repass']){
                $u_id=$user->add($data);
                if($u_id && $data['state']==1){
                    $data_id['u_id']=$u_id;
                    M('score')->add($data_id);
                }
                $this->redirect('index',3);
            }
        }else{
            $this->display();
        }
    }

    /*修改会员资料*/
    public function update(){
        if(IS_POST){
            $data['username'] = $_POST['username'];
            $data['sex'] = $_POST['sex'];
            $data['phone'] = $_POST['phone'];
            $data['address'] = $_POST['address'];
            $data['code'] = $_POST['code'];
            $data['email'] = $_POST['email'];
            $data['state'] = $_POST['state'];
            $data['id'] = $_POST['id'];
            $user = D('User');
            $user->save($data); 
            $this->redirect('index',3);
        }else{
            $id = $_GET['id'];
            $user = D('User');
            $data = $user->find($id);
            $this->assign('data',$data);
            $this->display();
        }
    }

    /*用户信息小窗*/
    public function show(){
        $data['id'] = $_GET['id'];
        $user = D('User');
        $info = $user->where($data)->getInfo();
        $this->assign('info',$info);
        $this->display();
    }

}
